<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 */
require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/confs/sysConf.php';

class GlobalParamsController {

	private $objTime;
	private $id;
	private $authorizeObj;
	private $db = "";
	private $RuleValue = "";
	private $SalaryCache = "";
	
	public function __construct() {
		include(ROOT_PATH . '/customConfig/salary.config.php');
		$this->SalaryCache = $_CfgSalary;
		$this->db = new DMLFunctions();
		error_reporting(0);
	}

	public function __distruct() {
		$this->db->dbDisconnect();
	}
	
	//获取薪资配置
	public function getGlobalParams($key = "") {
		static $RuleValue = null;
		if ($RuleValue === null) {
			$sql = "select * from hs_hr_sysconfigs where `key`='GlobalParams'";
			$query = $this->db->executeQuery($sql);
			$result = mysql_fetch_array($query, MYSQL_ASSOC);
			if($result['value'] != "") {
				$RuleValue = unserialize($result['value']);
			} else {
				$RuleValue = array();
			}
		}
		
		if($key != "") {
			return $RuleValue[$key];
		} else {
			return $RuleValue;
		}
	}
	/**
	 *薪资规则显示 
	 **/
	public function ShowGlobalParams() {
		$records = $this->getGlobalParams();
		//print_r($records);
		//die();
		$path = "/templates/eimadmin/ShowGlobalParams.php";
		$template = new TemplateMerger($records, $path);
		$template->display();
	}

	/**
	 *全局参数保存 
	 **/
	public function SaveGlobalParams() {
		
		$value = serialize($_POST);
		//保存数据
		
		$sql = "select * from hs_hr_sysconfigs where `key`='GlobalParams'";
		$query = $this->db->executeQuery($sql);
		$records = mysql_fetch_array($query, MYSQL_ASSOC);
		if($records == "") {
			$sql = "insert into hs_hr_sysconfigs (`key`,`value`,`addtime`) values ('GlobalParams','{$value}',".time().")";
			$resulq = $this->db->executeQuery($sql);
		} else {
			$sql = "update hs_hr_sysconfigs set `value` = '{$value}' where `key`='GlobalParams'";
			$resulq = $this->db->executeQuery($sql);
		}
		$this->redirect("保存成功", "?uniqcode=GlobalParams&menu_no_top=eim");
	}

	/**
	 *获取搜索的sqlwhere 
	 **/
	public function GetSearchSlqWhere($year,$month,$code="",$name="") {
		$where = "";
		$loc_code = isset($_POST['loc_code'])?htmlspecialchars(trim($_POST['loc_code'])):"";
		$loc_name = isset($_POST['loc_name'])?htmlspecialchars(trim($_POST['loc_name'])):"";
		if($code != "") $loc_code = $code;
		if($name != "") $loc_name = $name;
		if($loc_name != "") {
			if($loc_name == "emp_firstname") {
				$where .= " and {$loc_code} like '%{$loc_name}%' ";
			} else {
				$where .= " and b.{$loc_code} = '{$loc_name}' ";
			}
		}
		return $where;
	}
	

	/**
	 *获取员工名字和id
	 **/
	public function GetEmpName() {
		
		$EmpName = $_POST['EmpName'];
		$tbname = $_POST['tbname'];
		$EmpNameId = $_POST['EmpNameId'];
		$div = $_POST['div'];
		$EmpIdId = $_POST['EmpIdId'];
		$sql = "select * from  hs_hr_employee  where emp_firstname like '%{$EmpName}%'";
		//echo $sql;
		//die();
		$query = $this->db->executeQuery($sql);
		
		$str ='<tr style="cursor:default" onmousemove="$(this).css(\'background\-color\',\'#FFCCFF\')" onmouseout="$(this).css(\'background\-color\',\'\')"><td center="left" onclick="$(\'#'.$_POST["div"].'\').hide();">关闭</td></tr>';
        
		while($records = mysql_fetch_array($query, MYSQL_ASSOC))
	    {
	    	$records["emp_firstname2"] = str_replace($EmpName,'<font color=red>'.$EmpName.'</font>',$records["emp_firstname"]);
	    	$str .= '<tr style="cursor:default" onmousemove="$(this).css(\'background\-color\',\'#FFCCFF\')" onmouseout="$(this).css(\'background\-color\',\'\')"><td center="left" onclick="showproname(\''.$records["emp_firstname"].'\',\''.$records["emp_number"].'\',\''.$EmpIdId.'\',\''.$EmpNameId.'\',\''.$div.'\');">'.$records["emp_firstname2"].'</td></tr>';
	    }
	    if ($str != "")
	    {
	    	echo $str;
	    }
	    else {
	    	echo "no";
	    }
	    exit;
	}
	
	public function redirect($message=null, $url = null) {

		if (isset($url)) {
			$mes = "";
			if (isset($message)) {
				$mes = "&message=";
			}
			$url=array($url.$mes);
			$id="";
		} else if (isset($message)) {
			preg_replace('/[&|?]+id=[A-Za-z0-9]*/', "", $_SERVER['HTTP_REFERER']);

			if (preg_match('/&/', $_SERVER['HTTP_REFERER']) > 0) {
				$message = "&message=".$message;
				$url = preg_split('/(&||\?)message=[A-Za-z0-9]*/', $_SERVER['HTTP_REFERER']);
			} else {
				$message = "?message=".$message;
			}

			if (isset($_REQUEST['id']) && !empty($_REQUEST['id']) && !is_array($_REQUEST['id'])) {
				$id = "&id=".$_REQUEST['id'];
			} else {
				$id="";
			}
		} else {
			if (isset($_REQUEST['id']) && !empty($_REQUEST['id']) && (preg_match('/&/', $_SERVER['HTTP_REFERER']) > 0)) {
				$id = "&id=".$_REQUEST['id'];
			} else if (preg_match('/&/', $_SERVER['HTTP_REFERER']) == 0){
				$id = "?id=".$_REQUEST['id'];
			} else {
				$id="";
			}
		}

		header("Location: ".$url[0].$message.$id);
	}
}

?>